using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using GameLib.Enums;
using GameLib.Managers;
using Microsoft.Phone.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace _15_puzzle_camera_wp8
{
    

    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Game
    {
        private GraphicsDeviceManager _graphics;
        private SpriteBatch _spriteBatch;
        //ContentManager contentManager;
        private SpriteFont _infoFont;

        private MenuManager _menuManager;
        private InputManager _inputManager;
        private GameState _gameState;

        public Game1()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);
            // Extend battery life under lock.
            InactiveSleepTime = TimeSpan.FromSeconds(1);

            //contentManager = (Application.Current as App).Content;
            //contentManager = new ContentManager(this.Services);

            //infoFont = contentManager.Load<SpriteFont>("MenuFont");


            
            _gameState = GameState.StartMenu;
        }

        protected override void Initialize()
        {
            

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            //var singleton = Singleton.Instance;
            //singleton.DeviceWidth = GraphicsDevice.DisplayMode.Width;
            //singleton.DeviceHeight = GraphicsDevice.DisplayMode.Height;

            //var infoBox = Content.Load<Texture2D>("selectRectangle");

            _inputManager = new InputManager(Content);
            _menuManager = new MenuManager(Content);
            _menuManager.GameStart += _menuManager_GameStart;

            Debug.WriteLine("Width: " + GraphicsDevice.DisplayMode.Width);
            Debug.WriteLine("Height: " + GraphicsDevice.DisplayMode.Height);

            
            
        }

        void _menuManager_GameStart(object sender, EventArgs e)
        {
            _gameState = GameState.CaptureCameraPhoto;
            Debug.WriteLine("Event: GameStart - from MenuManager");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();



            switch (_gameState)
            {
                case GameState.CaptureCameraPhoto:
                    var cameraMgr = new CameraManager();
                    cameraMgr.CaptureEvent += cameraMgr_CaptureEvent;
                    cameraMgr.CaptureAborted += cameraMgr_CaptureAborted;
                    cameraMgr.Capture();
                    _gameState = GameState.WaitForCamera;
                    break;
                case GameState.WaitForCamera:

                    break;
                case GameState.StartMenu:
                    _menuManager.Update(gameTime.ElapsedGameTime);
                    //gameState = GameState.InGame;
                    //_inputManager.StartGame();
                    break;
                case GameState.InGame:
                    _inputManager.OnUpdate(gameTime.ElapsedGameTime);

                    break;
                case GameState.PauseMenu:
                    //menuMgr.Update(GameState.PauseMenu);

                    break;
                case GameState.Pause:

                    break;
                case GameState.GameFinished:
                    _inputManager.OnUpdate(gameTime.ElapsedGameTime);
                    //if (enterHighscore)
                    //{
                    //    enterHighscore = false;
                    //    var name = "";
                    //    var result = Dialog.InputBox("Ny Highscore!!!", "Skriv in ditt namn", ref name);
                    //    Debug.WriteLine("Highscore-namn: " + name);
                    //    if (result == System.Windows.Forms.DialogResult.OK)
                    //        scoreMgr.SaveScore(name);
                    //    else
                    //        scoreMgr.SaveScore("Unknown");
                    //    menuMgr.HighScore = scoreMgr.Scores;
                    //}
                    break;
            }

            base.Update(gameTime);
        }

        void cameraMgr_CaptureAborted(object sender, EventArgs e)
        {
            _gameState = GameState.StartMenu;
        }

        private void cameraMgr_CaptureEvent(object sender, PhotoResult e)
        {
            var tx2D = Texture2D.FromStream(GraphicsDevice, e.ChosenPhoto, 600, 375, false);
            _inputManager.setBackgroundFromCamera(tx2D);

            _inputManager.StartGame();
            _gameState = GameState.InGame;
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.WhiteSmoke);
            _spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
            //_spriteBatch.Begin();

            
           

            switch (_gameState)
            {
                case GameState.StartMenu:
                    _menuManager.Draw(_spriteBatch);
                    break;
                case GameState.PauseMenu:
                    _menuManager.Draw(_spriteBatch);
                    break;
                case GameState.InGame:
                    _inputManager.Draw(_spriteBatch);
                    break;
                case GameState.Pause:
                    break;
                case GameState.GameFinished:
                    break;
                case GameState.CaptureCameraPhoto:
                    break;
                case GameState.WaitForCamera:
                    break;
                default:
                    break;
            }

            _spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
